package com.yx.filesystem.monitor;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import com.yx.filesystem.client.ServerNode;
import com.yx.filesystem.client.StorageClientManager;
import org.eclipse.wb.swt.SWTResourceManager;

public class ServerNodeGroup extends Composite {

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ServerNodeGroup(Composite parent, int style) {
		super(parent, SWT.BORDER);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		setLayout(new GridLayout(4,true));
		List<ServerNode> serverList = StorageClientManager.getStorageServers();
		List<ServerNodeDisplay> ServerNodeDisplayList = new ArrayList<ServerNodeDisplay>();
		for (int i = 0; i < serverList.size(); i++) {
			ServerNodeDisplay ServerNodeDisplay = new ServerNodeDisplay(this,
					SWT.NONE, serverList.get(i));
			ServerNodeDisplayList.add(ServerNodeDisplay);
		}
		this.pack();
		(new StateMonitorThread(ServerNodeDisplayList)).start();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public class StateMonitorThread extends Thread {
		List<ServerNodeDisplay> ServerNodeDisplayList = null;

		public StateMonitorThread(List<ServerNodeDisplay> ServerNodeDisplayList) {
			this.ServerNodeDisplayList = ServerNodeDisplayList;
		}

		public void run() {
			while (true) {
				for (int i = 0; i < ServerNodeDisplayList.size(); i++) {
					try {
						final ServerNodeDisplay serverNodeDisplay = ServerNodeDisplayList
								.get(i);
						getDisplay().syncExec(new Runnable() {
							@Override
							public void run() {
								serverNodeDisplay.refreashState();
							}
						});
					} catch (Exception e) {
					}
				}
				synchronized (this) {
					try {
						sleep(30000);
					} catch (Exception e) {
					}
				}
			}
		}

	}

}
